﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frontend.Grids;
using SpeakupBackend;
using SpeakupBackend.Themes;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Frontend
{
    class GroupThemeGrid: HighlightableGrid
    {
        static Dictionary<GroupThemeName, Brush> InactiveNameControls;
        static Dictionary<GroupThemeName, Brush> ActiveNameControls;

        static GroupThemeGrid()
        {
            InactiveNameControls = new Dictionary<GroupThemeName, Brush>();
            ActiveNameControls = new Dictionary<GroupThemeName, Brush>();

            foreach (GroupThemeName name in Enum.GetValues(typeof(GroupThemeName)))
            {
                Color color;
                switch (name)
                { 
                    case GroupThemeName.Blue:
                        color = Colors.LightBlue;
                        break;
                    case GroupThemeName.Orange:
                        color = Colors.Orange;
                        break;
                    case GroupThemeName.Red:
                        color = Colors.Red;
                        break;
                    case GroupThemeName.Violet:
                        color = Colors.Violet;
                        break;
                    case GroupThemeName.Yellow:
                        color = Colors.Yellow;
                        break;
                    default:
                        color = Colors.Gray;
                        break;
                }

                InactiveNameControls.Add(name, new SolidColorBrush(Color.FromArgb(40, color.R, color.G, color.B)));
                ActiveNameControls.Add(name, new SolidColorBrush(Color.FromArgb(255, color.R, color.G, color.B)));
            }
        }

        public GroupThemeGrid()
        {
            HighlightBrush = Brushes.LightPink;

            Color color = Colors.LightGreen;
            CurrentBrush = new SolidColorBrush(Color.FromArgb(100, color.R, color.G, color.B));
        }

        BackendContext m_backendCtx;
        public BackendContext BackendCtx
        {
            get
            {
                return m_backendCtx;
            }
            set
            {
                if (m_backendCtx != value)
                {
                    m_backendCtx = value;                    
                }
            }
        }

        Dictionary<GroupThemeName, Border> NameControls;
        List<GroupThemeName> m_groupThemeNames;
        List<GroupThemeName> GroupThemeNames
        {
            get
            {
                if (m_groupThemeNames == null)
                {
                    m_groupThemeNames = new List<GroupThemeName>();
                    foreach (GroupThemeName name in Enum.GetValues(typeof(GroupThemeName)))
                    {
                        m_groupThemeNames.Add(name);
                    }
                }

                return m_groupThemeNames;
            }
        }

        List<Level> m_levels;
        List<Level> Levels
        {
            get
            {
                if (m_levels == null)
                {
                    m_levels = Level.GetLevels().ToList();
                }
                return m_levels;
            }
        }

        int IndexOf(GroupThemeName name)
        {
            return GroupThemeNames.IndexOf(name) + 1;
        }

        int IndexOf(Level level)
        {
            return Levels.IndexOf(level) + 1;
        }

        UIElementFactory UIElementFactory = new UIElementFactory();        

        public void SetupGrid()
        {
            Children.Clear();

            SetupRowHeaders();
            SetupColumnHeaders();
            SetupData();

            CurrentColumn = IndexOf(Level.GetLevel(BackendCtx.Stats.Level));
        }

        void SetupRowHeaders()
        {
            RowDefinitions.Clear();

            for (int i = 0; i < GroupThemeNames.Count + 1; i++)
            {
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = new GridLength(1, GridUnitType.Star);
                RowDefinitions.Add(rowdef);
            }


            NameControls = new Dictionary<GroupThemeName, Border>();

            foreach (GroupThemeName name in GroupThemeNames)
            {
                Border element = UIElementFactory.CreateGroupThemeName(name);
                Grid.SetRow(element, IndexOf(name));
                Grid.SetColumn(element, 0);

                NameControls.Add(name, element);
                element.Background = InactiveNameControls[name];

                Children.Add(element);
            }
        }

        void SetupColumnHeaders()
        {
            ColumnDefinitions.Clear();

            for (int i = 0; i < Levels.Count + 1; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = new GridLength(1, GridUnitType.Star);
                ColumnDefinitions.Add(coldef);
            }

            foreach (Level level in Levels)
            {
                UIElement element = UIElementFactory.CreateLevel(level);
                Grid.SetColumn(element, IndexOf(level));
                Grid.SetRow(element, 0);
                Children.Add(element);
            }
        }

        void SetupData()
        {
            foreach (GroupTheme theme in BackendCtx.GroupThemes)
            {
                UIElement element = UIElementFactory.CreateGroupTheme(theme);
                Grid.SetRow(element, IndexOf(theme.GroupThemeName));
                Grid.SetColumn(element, IndexOf(theme.Level));
                Children.Add(element);
            }
        }

        protected override void OnHighlightChanged()
        {
            foreach (GroupThemeName name in Enum.GetValues(typeof(GroupThemeName)))
            {
                if (HighlightedRow == IndexOf(name))
                {
                    NameControls[name].Background = ActiveNameControls[name];
                    NameControls[name].SetValue(TextBlock.FontSizeProperty, 15.0);
                }
                else
                {
                    NameControls[name].Background = InactiveNameControls[name];
                    NameControls[name].ClearValue(TextBlock.FontSizeProperty);
                }
            }
            
        }
        
    }

}
